﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Model;
using MM.Common;

namespace MM.LibraryAPI.Task
{
    public class Common
    {
        /// <summary>
        /// 取联网时间间隔
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="APPID"></param>
        /// <returns></returns>
        public static string GetTimer(string PID, string APPID)
        {
            object objTimer = DbHelperSql.GetSingle(string.Format("SELECT Timer FROM T_Timers WHERE PID='{0}' AND APPID='{1}'", PID, APPID));
            return objTimer.ToStringOr();
        }
        public static List<MM.Model.Rules> GetRules(string imsimac, string pid, string appID)
        {
            List<MM.Model.Rules> result = null;
            using (SqlConnection conn = new SqlConnection(DbHelperSql.connectionString))
            {
                SqlParameter[] parameters = { 
                       new SqlParameter("@imsimac", SqlDbType.VarChar),
                       new SqlParameter("@pid", SqlDbType.VarChar),
                       new SqlParameter("@appID", SqlDbType.VarChar)};
                parameters[0].Value = imsimac;
                parameters[1].Value = pid;
                parameters[2].Value = appID;

                using (SqlDataReader dr = DbHelperSql.ExecuteReaderSP(conn, "GetTask", parameters))
                {
                    if (dr != null && dr.HasRows)
                    {
                        result = DTOHelper.CreateModelList<MM.Model.Rules>(dr);
                        if (result != null && result.Count > 0)
                        {
                            dr.NextResult();
                            List<MM.Model.Task> LMaskInfo = DTOHelper.CreateModelList<MM.Model.Task>(dr);
                            result[0].LTask = LMaskInfo;
                        }
                    }
                    else
                    {
                        result = new List<MM.Model.Rules>();
                    }
                }
            }
            return result;
        }
    }
}
